var util = require('/utils/util.js')

//app.js
App({
  onLaunch: function() {
    console.log('小程序开始运行');
    this.init();
  },
  onShow: function() {
    console.log('在此小程序中');
  },
  onHide: function() {
    console.log('不在此小程序中');
  },
  onError: function(msg) {
    console.log('有错误:' + msg);
  },
  config: {
    //接口host
    host: 'https://zhaopin.caikawangluo.com',
    //版本
    version: "0.1.0",
    //app名称
    channel: '财税招聘求职信息平台',
    //默认联系方式
    defaultContact: 'hr@email.com'
  },
  init: function() {
    var token = wx.getStorageSync('token');
    var token_expiration = wx.getStorageSync('token_expiration');
    var city = wx.getStorageSync('city');
    var timestamp = Date.parse(new Date());
    if (token && token_expiration && token_expiration > timestamp) {
      //取设备信息
      this.globalData.systemInfo = wx.getSystemInfoSync();
      wx.setStorageSync('systemInfo', this.globalData.systemInfo);
      this.globalData.token = token;
      this.globalData.city = city;
    } else {
      this.globalData.token = null;
    }
  },
  globalData: {
    //设备信息
    systemInfo: null,
    //微信用户唯一id
    token: null,
    //微信用户信息
    userInfo: null,
    city:null,
  },
  apiList: {
    login: '/api/login', //登录接口
    uploadImage: '/api/upload/image', //上传图片接口

    industry: '/api/dict/industry', //行业数据
    typelist: '/api/dict/typelist', //字典列表
    template: '/api/dict/template', //模版列表
    citys: '/api/dict/city', //城市列表
    cityslv: '/api/dict/citylv', //省市两级结构
    searchRecommend: '/api/search/recommend', //猜你喜欢

    getApplyInfo: '/api/apply/info', //获取审核状态			
    joinPersonal: '/api/apply/check/personal', //个人入驻
    joinEnterprise: '/api/apply/check/enterprise', //企业入驻
    joinInvitation: '/api/apply/check/code', //内部邀请码
    createJob: '/api/job/create', //创建新职位
    getMyJobs: '/api/job/myjobs', //获取我的职位
    pushMyJob: '/api/job/push', //发布我的职位
    refreshMyJob: '/api/job/refresh', //刷新我的职位
    editMyJob: '/api/job/edit', //编辑我的职位
    offMyJob: '/api/job/offline', //下架我的职位
    delMyJob: '/api/job/del', //删除我的职位
    search: '/api/search', //搜索
    positionDetail: '/api/job/detail', //岗位详情
    index: '/api/index', //首页
    editPersonal: '/api/user/info/edit', //个人信息编辑
    getPersonal: '/api/user/info', //获取个人信息
    collections: '/api/user/position/collections', //收藏职位
    collectionsList: '/api/user/position/collections/list', //收藏列表

    getEmailCount: '/api/job/get_email_count', //获取职位联系方式计数接口
    getShareCount: '/api/job/share_count', //分享群或朋友记数
    getScanLog: '/api/scanlog/position', //扫码日志

  },
  apiGet: function(url, data, callback) {
    data.token = this.globalData.token;
    wx.request({
      url: this.config.host + url,
      data: data,
      method: 'GET',
      dataType: 'json',
      header: {
        'content-type': 'application/x-www-form-urlencoded;charset=UTF-8'
      },
      success: function(res) {
        callback(res.data)
        var timestamp = Date.parse(new Date());
        var expiration = timestamp + 20 * 60 * 1000
        wx.setStorageSync("token_expiration", expiration)
      },
      fail: function(res) {
        console.log(url + '请求失败')
      },
      complete: function(res) {
        console.log(url + '请求完成')
        console.log(res);
      }
    })
  },
  apiPost: function(url, data, callback) {
    data.token = this.globalData.token;
    wx.request({
      url: this.config.host + url,
      data: data,
      method: 'POST',
      dataType: 'json',
      header: {
        "content-type": "application/x-www-form-urlencoded;charset=UTF-8"
      },
      success: function(res) {
        callback(res.data)
        var timestamp = Date.parse(new Date());
        var expiration = timestamp + 20 * 60 * 1000
        wx.setStorageSync("token_expiration", expiration)
      },
      fail: function(res) {
        console.log(url + '请求失败')
      },
      complete: function(res) {
        console.log(url + '请求完成')
        console.log(res);
      }
    })
  },
  alert: function(msg) {
    wx.showModal({
      content: msg,
      showCancel: false,
      confirmColor: '#41a4f4'
    });
  },
  loading: function() {
    wx.showLoading({
      title: '加载中...',
      mask: true
    });
  },
  hideloading: function() {
    wx.hideLoading();
  },
  util
})